package org.gruposp2p.proyectokarma.gwt.client.ui.document;

import org.gruposp2p.proyectokarma.gwt.client.json.JSONToDomain;
import org.gruposp2p.proyectokarma.gwt.client.panel.SizedTagsPanel;
import org.gruposp2p.proyectokarma.gwt.client.ui.ErrorDialog;
import org.gruposp2p.proyectokarma.gwt.client.util.RequestHelper;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SearchDocumentPanel extends Composite implements SizedTagsPanel.Handler {

	private static SearchDocumentUiBinder uiBinder = GWT.create(SearchDocumentUiBinder.class);
	interface SearchDocumentUiBinder extends UiBinder<Widget, SearchDocumentPanel> { }

	@UiField VerticalPanel mainPanel;
    @UiField Button searchButton;
    @UiField TextBox searchText;
    @UiField VerticalPanel tagsPanelContainer;
    DocumentsTablePanel documentsTablePanel;
	
	public SearchDocumentPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		searchButton.setStylePrimaryName("searchButton");
		documentsTablePanel = new DocumentsTablePanel();
		documentsTablePanel.getElement().getStyle().setMarginTop(10, Unit.PX);
		SizedTagsPanel tagsPanel = new SizedTagsPanel();
		tagsPanel.setTagPanelHandler(this);
		tagsPanelContainer.add(tagsPanel);
	}
	
    @UiHandler("searchButton")
    void handleCancelButton(ClickEvent e) {
    	String textQuery = searchText.getText().trim();
    	if (!"".equals(textQuery)){
        	RequestHelper.doGet(ServerPaths.getSearchDocumentPath(textQuery), 
        			new ServerRequestCallback());
    	}
    	else mainPanel.remove(documentsTablePanel);
    }

	@Override
	public void handleSelectedTag(String selectedTag) {
		searchText.setText(selectedTag);
    	RequestHelper.doGet(ServerPaths.getSearchDocumentByTagPath(selectedTag), 
    			new ServerRequestCallback());
	}
	
    private class ServerRequestCallback implements RequestCallback {

        @Override
        public void onError(Request request, Throwable exception) {
        	new ErrorDialog().show("Exception", exception.getMessage());
        	Log.error(exception.getMessage());                
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
            Log.debug("response.getText(): " + response.getText());
            if (response.getStatusCode() == Response.SC_OK) {
            	JSONValue jsonValue = JSONParser.parseLenient(response.getText());
                documentsTablePanel.setDocuments(JSONToDomain.getDocuments(jsonValue));
                mainPanel.add(documentsTablePanel);
            } else {
            	new ErrorDialog().show("String.valueOf(response.getStatusCode())", response.getText());
            }
        }

    }

}
